'use strict';

/**
 * @ngdoc service
 * @name jgappApp.pageservice
 * @description
 * # pageservice
 * Service in the jgappApp.
 */
angular.module('jgappApp')
    .service('PagingService', function ($http, Util, $filter) {
        var Pagingservice = function (url, options,config) {
            this.items = [];
            this.porlist = {};
            this.valuePropertys = {};
            this.busy = false;
            this.limit = 10;
            this.start = 0;
            this.pageNo = 0;
            this.noData = true;
            this.tip = "";
            this.url = url;
            this.options = options || {};
            this.nextPage();
            this.count = 0;
            this.title = "";
            this.config = config||{};
        };
        Pagingservice.prototype.nextPage = function () {
            if (!this.noData) {
                return;
            }
            if (this.busy) {
                return;
            }
            this.busy = true;
            this.options.limit =this.options.limit|| this.limit
            this.start = (this.pageNo++) * this.options.limit;
            this.options.start = this.start
            $http.get(this.url, {
                params: this.options
            }).
                success(function (data, status, headers, config) {
                    if (data.logout) {
                        Util.logout();
                        return;
                    }
                    var items = data.dataList;
                    if (items) {
                        for (var i = 0; i < items.length; i++) {
                            this.items.push(items[i]);
                        }
                    }
                    this.porlist = data.displayPropertys||this.config.porlist;
                    this.valuePropertys = data.valuePropertys;

                    if (data.success) {
                        // 获取搜索关键字
                        this.title = data.searchName||this.config.searchName;
                        // 如果没有数据则返回
                        if (!data.dataList) {
                            this.noData = false;
                            this.tip = "没有数据";
                            return;
                        }
                        var items = data.dataList;
                        this.count = data.totalCount;
                        if (this.limit > items.length) {
                            this.noData = false;
                            if (this.items.length < 1) {
                                this.tip = "没有数据";
                            }
                            return
                        }
                    } else {
                        this.noData = false;
                        this.tip = "没有数据";
                    }
                    this.busy = false;
                }.bind(this)).
                error(function (data, status, headers, config) { }.bind(this));
        };
        return Pagingservice;

    });
